﻿using System;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace CodeTester
{
    public partial class Form1 : Form
    {
        public string irr,ptr;
        public int ir = 2;
        int i = 0;
        public static string[] array;

        public Form1(string tt,string funcPt)
        {
            InitializeComponent();
            richTextBoxMain.Text = tt;
            this.ptr = funcPt;
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            updateLabelRowIndex();

            i++;

            int index = this.richTextBoxMain.SelectionStart;    //记录修改的位置

            listBox1.Items.Clear();

            if (i == 4)
            {
                this.richTextBoxMain.SelectAll();
                this.richTextBoxMain.SelectionColor = Color.Black;

                i = 0;
            }
            try
            {
                array = richTextBoxMain.Text.Split(' ');
            }
            catch
            {
                this.richTextBoxMain.Select(index, 0);
            }

            if (array[array.Length - 1] == "") { this.richTextBoxMain.Select(index, 0); return; };


            string head = array[array.Length - 1].Substring(0, 1);
            Regex regex = new Regex($"^{head}.*"); //正则表达寻找
            foreach (var item in KeyWord.mcKeyWord)
            {
                Match match = regex.Match(item);
                string temp = match.Value;
                if (temp != "")
                {
                    listBox1.Items.Add(temp);
                }
                else
                {
                    this.richTextBoxMain.Select(index, 0);
                }
            }
            string[] keystr = KeyWord.mcKeyWord;

            for (int i = 0; i < keystr.Length; i++)
                getbunch(keystr[i], richTextBoxMain.Text, Color.Blue, Color.Green);

            keystr = KeyWord.mcSelector; //定位至选择器

            for (int i = 0; i < keystr.Length; i++)
                getbunch(keystr[i], richTextBoxMain.Text, Color.Purple, Color.LightBlue);

            keystr = KeyWord.mcChar; //定位至特殊符

            for (int i = 0; i < keystr.Length; i++)
                getbunch(keystr[i], richTextBoxMain.Text, Color.Orange, Color.Red);

            keystr = KeyWord.mcUse; //定位至选择符

            for (int i = 0; i < keystr.Length; i++)
                getbunch(keystr[i], richTextBoxMain.Text, Color.Red, Color.LightPink);

            this.richTextBoxMain.Select(index, 0);     //返回修改的位置
        }

        //染色
        public int getbunch(string p, string s, Color lightColor, Color nLightColor)  //给关键字上色
        {
            int cnt = 0; int M = p.Length; int N = s.Length;
            char[] ss = s.ToCharArray(), pp = p.ToCharArray();
            if (M > N) return 0;
            for (int i = 0; i < N - M + 1; i++)
            {
                int j;
                for (j = 0; j < M; j++)
                {
                    if (ss[i + j] != pp[j]) break;
                }
                if (j == p.Length)
                {
                    richTextBoxMain.Select(i, p.Length);
                    this.richTextBoxMain.SelectionColor = lightColor;
                    cnt++;
                }
            }
            return cnt;
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            richTextBoxMain.Text += listBox1.Items[listBox1.SelectedIndex].ToString().Substring(1);
        }

        /// <summary>
        /// 更新行号
        /// </summary>
        private void updateLabelRowIndex()
        {
            //we get index of first visible char and number of first visible line
            Point pos = new Point(0, 0);
            int firstIndex = this.richTextBoxMain.GetCharIndexFromPosition(pos);
            int firstLine = this.richTextBoxMain.GetLineFromCharIndex(firstIndex);

            //now we get index of last visible char and number of last visible line
            pos.X += this.richTextBoxMain.ClientRectangle.Width;
            pos.Y += this.richTextBoxMain.ClientRectangle.Height;
            int lastIndex = this.richTextBoxMain.GetCharIndexFromPosition(pos);
            int lastLine = this.richTextBoxMain.GetLineFromCharIndex(lastIndex);

            //this is point position of last visible char, 
            //we'll use its Y value for calculating numberLabel size
            pos = this.richTextBoxMain.GetPositionFromCharIndex(lastIndex);

            labelRowIndex.Text = "";
            for (int i = firstLine; i <= lastLine + 1; i++)
            {
                if (i >= 9)
                {
                    labelRowIndex.Text += i + 1 + "\r\n";
                }
                else
                {
                    labelRowIndex.Text += "0" + (i + 1).ToString() + "\r\n";
                }
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            i++;

            int index = this.richTextBoxMain.SelectionStart;    //记录修改的位置

            listBox1.Items.Clear();

            if (i == 4)
            {
                this.richTextBoxMain.SelectAll();
                this.richTextBoxMain.SelectionColor = Color.Black;

                i = 0;
            }
            try
            {
                array = richTextBoxMain.Text.Split(' ');
            }
            catch
            {
                this.richTextBoxMain.Select(index, 0);
            }

            if (array[array.Length - 1] == "") { this.richTextBoxMain.Select(index, 0); return; };

            listBox1.ItemHeight = 25;
            //染色
            string[] keystr = KeyWord.mcKeyWord;

            for (int i = 0; i < keystr.Length; i++)
                getbunch(keystr[i], richTextBoxMain.Text, Color.Blue, Color.Green);

            keystr = KeyWord.mcSelector; //定位至选择器

            for (int i = 0; i < keystr.Length; i++)
                getbunch(keystr[i], richTextBoxMain.Text, Color.Purple, Color.LightBlue);

            keystr = KeyWord.mcChar; //定位至特殊符

            for (int i = 0; i < keystr.Length; i++)
                getbunch(keystr[i], richTextBoxMain.Text, Color.Orange, Color.Red);

            keystr = KeyWord.mcUse; //定位至选择符

            for (int i = 0; i < keystr.Length; i++)
                getbunch(keystr[i], richTextBoxMain.Text, Color.Red, Color.LightPink);
        }

        private void richTextBoxMain_VScroll(object sender, EventArgs e)
        {
            //move location of numberLabel for amount of pixels caused by scrollbar
            int p = richTextBoxMain.GetPositionFromCharIndex(0).Y % (richTextBoxMain.Font.Height + 1);
            labelRowIndex.Location = new Point(0, p);
            updateLabelRowIndex();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //saveFileDialog1.Title = "保存Func文件";
            //saveFileDialog1.Filter = "MineCraft Function files(*.mcfunction)|*.mcfunction";
            //DialogResult result = saveFileDialog1.ShowDialog();
            //if (result == DialogResult.OK)  //DialogResult为枚举类,OK为保存
            //{
            //    //获得文件路径
            //    string localFilePath = saveFileDialog1.FileName.ToString();
            //    StreamWriter sw = new StreamWriter(localFilePath);
            //    sw.Write(richTextBox1.Text);
            //    sw.Flush();
            //    sw.Close();
            //}
            StreamWriter sw = new StreamWriter(ptr);
            sw.Write(richTextBoxMain.Text);
            sw.Flush();
            sw.Close();
            MessageBox.Show($@"已经保存至{ptr}!");
            this.Close();
        }
    }
}
